package mypackage.utils;

import mypackage.bean.*;
import mypackage.servicce.*;
import mypackage.servicce.Impl.*;


import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author YH925
 * @create 2021/12/6 20:45
 */
public class Views {

    private final ClientsService clientsService = new ClientsServiceImpl();
    private final GoodsService goodsService = new GoodsServiceImpl();
    private final AddressService addressService = new AddressServiceImpl();
    private final HistoryOrderService historyOrderService = new HistoryOrderServiceImpl();
    private final GoodOrderService goodOrderService = new GoodOrderServiceImpl();
//    private final AdminView adminView = new AdminView();
    private final AdminService adminService = new AdminServiceImpl();
    private final ProfitsService profitsService = new ProfitsImpl();



    private int count = 3;


    //显示主页面
    public  void mainView(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("******************************************************************************");
        System.out.println("欢迎来到手机销售系统");
        System.out.println("--请先选择您的身份--");
        System.out.println("--1.管理员\t2.用户\t0.退出--");
        System.out.print("--选择对应的编号：");
        System.out.println("\n******************************************************************************");

        int choose = scanner.nextInt();
        switch (choose){
            case 0:
                System.exit(0);
                break;
            case 1:
                adminLogin();
                break;
            case 2: registerOrLoginMenu();
                break;
            default:
                System.out.println("本次输入有误，请重新输入");
                mainView();
        }
    }

    //第二级菜单，用户登录或注册

    /**
     * 用户登录或注册，用户注册成功后，跳转到登录页面
     * 用户登录后显示用户可用的菜单
     */

    public void registerOrLoginMenu(){
        System.out.println("******************************************************************************");

        System.out.println("欢迎来到用户中心");
        System.out.println("1.注册");
        System.out.println("2.登录");
        System.out.println("0.返回");
        System.out.println("-1.退出");
        System.out.println("******************************************************************************");

        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                clientRegister();
                break;
            case 2:
                clientLogin();
                break;
            case 0:
                mainView();
                break;
            case -1:
                System.exit(0);
                break;
            default:
                System.out.println("输入有误，重新输入");
                registerOrLoginMenu();
        }
    }

    /**
     * 用户注册的方法
     * 1.用户输入用户名和密码进行注册
     * 2.用户输入用户名和密码后，先向数据库查询用户名是否存在，如果存在则不能完成注册，跳转到登录注册菜单
     * 3.注册成功，跳转到登录界面
     */
    public void clientRegister(){
        System.out.println("********************注册页面******************");

        Scanner scanner = new Scanner(System.in);
        System.out.println("\n");
        System.out.print("输入用户名：");
        String username = scanner.next();

        //开始检测此用户名是否存在
        Clients client = clientsService.selectClientByName(username);
        if (client == null){
            //数据库中不存在此用户名，可以继续注册

            System.out.print("输入密码：");
            String password = scanner.next();
            System.out.print("确认密码：");
            String password2 = scanner.next();
            System.out.println("******************注册完成！***********************\n");

            if (!Objects.equals(password2, password)){
                //两次密码不一致，重新注册
                System.out.println("两次密码不一致，重新注册！");
                registerOrLoginMenu();
            }else {
                clientsService.clientRegister(username,password);
            }

        }else {
            //用户名已存在，不能完成注册，提示用户，调回登录注册页面
            System.out.println("用户名已经存在，重新注册。。。");
            registerOrLoginMenu();
        }

    }

    /**
     * 用户登录方法
     * 1.用户有三次输错密码的机会，机会耗尽，退出系统，
     * 2.登录成功，跳转到用户菜单界面
     * 3.
     */

    int clientID = 0;

    public void clientLogin(){

        Clients clients = new Clients();
        System.out.print("请输入用户名：");
        Scanner scanner = new Scanner(System.in);
        clients.setName(scanner.next());
        System.out.print("请输入密码：");
        clients.setPassword(scanner.next());

        Clients clients1 = clientsService.clientsLogin(clients);

        if (clients1 != null){
            //此时登录成功
            System.out.println("登录成功！！！！");
            clientID = clients1.getId();
            clientsMenus();
            count = 3;
        }else {
            //此时登录失败
            while (true){
                --count;
                if (count > 0){
                    System.out.println("账号或密码错误！请重新输入！您还有::"+count+"::次机会");
                    clientLogin();
                }else {
                    System.out.println("次数耗尽，已退出！");
                    System.exit(0);
                }
            }

        }
    }

    /**
     * 用户菜单
     * 用户登录成功后显示的可用菜单
     */

    public void clientsMenus(){
        System.out.println("**************************************************************************");

        System.out.println("请输入指令进行操作");
        System.out.println("1.浏览商品！");
        System.out.println("2.查看个人信息");
        System.out.println("3.查看购物车");
        System.out.println("0.返回上一级");
        System.out.println("-1退出系统");
        System.out.println("**************************************************************************");

        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case -1:
                System.exit(0);
                break;
            case 0:
                mainView();
                break;
            case 1:
                listGoodsFromClientsViews();
                selectGoodsByExample();
                break;
            case 2:
                personalInfoMenu();
                break;
            case 3:
                lookGoodChart();
                break;
            default:
                System.out.println("输入有误，重新输入");
                clientsMenus();
        }
    }

    /**
     * 操作2：查看个人信息
     * 个人信息包括消费信息和收货地址
     *
     */

    public void personalInfoMenu(){

        int amount = historyOrderService.findConsumeAmount(clientID);
        double money = historyOrderService.findConsumeMoney(clientID);
        int addressAmount = addressService.findAddressAmount(clientID);
        System.out.println("*******************************************************************");

        System.out.println("您共购买过："+amount+"件商品\t共消费："+money+"元\t您有："+addressAmount+"个收货地址");
        System.out.println("\n*******************************************************************");


        System.out.println("1.查看消费记录");
        System.out.println("2.查看收货地址信息");
        System.out.println("0.返回上一级");
        System.out.println("-1.退出系统");
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                consumeRecord();
                personalInfoMenu();
                break;
            case 2:
                lookAddress();
                break;
            case 0:
                clientsMenus();
                break;
            case -1:
                System.exit(0);
                break;
            default:
                System.out.println("输入有误，重新输入！");
                personalInfoMenu();
        }
    }

    /**
     * 查看历史消费记录
     */
    public void consumeRecord(){
        System.out.println("\n*******************************************************************");

        System.out.println("消费记录如下：");
        List<ReceiveOrder> record = historyOrderService.findAllConsumeRecord(clientID);
        for (ReceiveOrder order : record) {
            System.out.println("编号："+order.getId()+" ,商品名："+order.getName()+",数量："+order.getAmount()+
                    ","+order.getPrice()+","+order.getSendAddress()+",时间："+order.getSaleTime()+
                    ",收件人："+order.getAcceptName()+",收件人号码"+order.getTel());
        }
        System.out.println("\n*******************************************************************");


    }

    /**
     * 查看收货地址信息
     */
    public void lookAddress(){
        System.out.println("收货地址如下：");
        List<Address> addressList = addressService.selectAddressList(clientID);
        for (Address address : addressList) {
            System.out.println("编号："+address.getId()+"\t收货地址："+address.getAcceptAddress()
            +"\t收件人："+address.getAcceptName()+"\t收件人号码："+address.getTel());
        }
        System.out.println("********************************************");
        addressView();

    }
    public void addressView(){
        System.out.println("******************************************************************************");

        System.out.println("选择您要进行的操作：");
        System.out.println("1.修改地址信息：");
        System.out.println("2.删除地址信息：");
        System.out.println("0.返回上一层：");
        System.out.println("-1.退出：");
        System.out.println("******************************************************************************");

        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                updateAddress();
                break;
            case 2:
                delAddress();
                break;
            case 0:
                personalInfoMenu();
                break;
            case -1:
                System.exit(0);
                break;
            default:
                System.out.println("输入有误，重新输入！");
                addressView();
        }
    }

    /**
     * 修改地址信息
     */
    public void updateAddress(){
        System.out.println("******************************************************************************");


        Scanner scanner = new Scanner(System.in);

        System.out.print("输入需要修改的地址编号：");
        int choose = scanner.nextInt();
        System.out.print("输入新的地址：");
        String newAddress = scanner.next();
        System.out.print("输入新的号码：");
        String newTel = scanner.next();
        System.out.print("输入新的名字：");
        String newName = scanner.next();

        addressService.updateInfo(choose,newAddress,newTel,newName);
        System.out.println("修改完成");
        lookAddress();
    }


    /**
     * 删除地址信息
     */
    public void delAddress(){
        System.out.print("输入地址编号：");
        Scanner scanner = new Scanner(System.in);
        int addressId = scanner.nextInt();

        addressService.delAddressById(addressId);
        System.out.println("删除成功！");
        lookAddress();
    }
    public void listGoodsFromClientsViews(){
        List<Goods> goods = goodsService.selectAllGoods();
        System.out.println("***************************商品信息如下**************************\n\n");
        for (Goods good : goods) {
            System.out.println("商品编号："+good.getId()+"\t商品名："+good.getName()+"\t价格："+good.getOutputMoney()+"\t品牌："+good.getBrand());
        }
        System.out.println("\n********************************************************************");

    }

    /**
     * 查看购物车根据用户id
     */
    public void lookGoodChart(){
        List<GoodOrder> chart= goodOrderService.findGoodChartWithGood(clientID);
        if (chart.isEmpty()){
            System.out.println("购物车内空空如也。。。");
            clientsMenus();
        }else {
            Clients client = clientsService.findClientById(clientID);
            System.out.println("************"+client.getName()+"购物车信息如下***************\n");
            for (GoodOrder order : chart) {
                System.out.println("编号："+order.getId()+"\t商品名："+order.getGoods().getName()+"\t数量："+order.getAmount()+
                        "\t价格："+order.getPrice()+"\t加入时间："+order.getTime());
            }
            System.out.println("数量："+goodOrderService.findTotalGoodsFromChart(clientID)+"\t总价："+goodOrderService.findTotalPriceFromChart(clientID));
            System.out.println("*******************************************************");
            goodChartMenu();
        }
    }

    /**
     * 购物车内可选择的操作
     */
    public void goodChartMenu(){
        System.out.println("***************************输入指令号完成操作***************************\n");
        System.out.println("1.购买全部商品");
        System.out.println("2.删除我不想要的商品");
        System.out.println("3.修改商品数量");
        System.out.println("0.返回上一级");
        System.out.println("-1.退出系统");
        System.out.println("**************************************************************************");
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case -1:
                System.exit(0);
                break;
            case 0:
                clientsMenus();
                break;
            case 1:
                buyAllGoodsFromChart();
                break;
            case 2:
                deleteMyDislikeGood();
                break;
            case 3:
                updateAmount();
                break;
            default:
                System.out.println("输入有误，重新输入！");
                goodChartMenu();
        }
    }

    /**
     * 操作1，购买全部商品
     * 将购物车内商品插入到历史订单内
     * 同时删除购物车内关于当前用户的商品
     */
    public void buyAllGoodsFromChart(){
        System.out.println("******************************************************************************");

        List<Address> addresses = addressService.selectAddressListWithClient(clientID);
        if (!addresses.isEmpty()){
            //收货地址列表不为空，用户选择收货地址，完成购买,并将购买信息写入历史订单中。
            System.out.println("输入收货地址编号，选择收货地址\n");
            for (Address address : addresses) {
                System.out.println("编号："+address.getId()+"\t用户名："+address.getClient().getName()+
                        "\t收件人姓名："+address.getAcceptName()+"\t收件人号码:"+address.getTel()+
                        "\t收件地址："+address.getAcceptAddress()
                );
            }
            System.out.println("******************************************************************************");

            //输入收货地址的id选择收货地址
            Scanner scanner = new Scanner(System.in);
            int addressId = scanner.nextInt();

            //收货地址已经选好了，将当前用户购物车中的所有商品加入历史订单中
            historyOrderService.insertDataFromChart(clientID,addressId);

            System.out.println("购买完成！");
            //购买完成后需要将购物车清空
            goodOrderService.deleteAllGoodsByClientId(clientID);

            listGoodsFromClientsViews();

        }else {
            //用户收货列表为空，则该用户没有填写过收货列表，先让用户填写收货地址，再完成购买
            System.out.println("您还没有收货地址，请先填写收货地址");
            String choose;
            do {
                Scanner scanner = new Scanner(System.in);
                System.out.print("请输入收货地址：");
                String acceptAddress = scanner.next();
                System.out.print("请输入收货人姓名：");
                String acceptName = scanner.next();
                System.out.print("请输入收件人号码：");
                String tel = scanner.next();
                addressService.insertAddress(clientID,acceptAddress,tel,acceptName);
                System.out.println("填写成功！");
                System.out.print("是否继续输入y/n:");
                choose = scanner.next();
            }while ("y".equalsIgnoreCase(choose));
        }
    }

    /**
     * 操作2：删除我不想要的商品
     */
    public void deleteMyDislikeGood(){
        System.out.println("******************************************************************************");


        List<GoodOrder> good = goodOrderService.findGoodChartWithGood(clientID);
        if (good.isEmpty()){
            System.out.println("当前购物车空空如也，多去浏览商品吧");
        }else {
            Scanner scanner = new Scanner(System.in);
            String choose;
            do {
                System.out.print("请输入商品的编号：");
                int id = scanner.nextInt();
                goodOrderService.deleteOneGoodById(id);
                System.out.println("是否继续y/n:");
                choose = scanner.next();
                System.out.println("删除成功！");
            }while ("y".equalsIgnoreCase(choose));
        }
        System.out.println("******************************************************************************");

        goodChartMenu();

    }

    /**
     * 操作3：修改购物车中商品的数量
     */
    public void updateAmount(){
        System.out.print("输入购物车内商品编号：");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        System.out.print("输入新的数量：");
        int newAmount = scanner.nextInt();
        goodOrderService.updateGoodAmount(id,newAmount);
        System.out.println("修改成功！");
        lookGoodChart();
    }

    //按照条件筛选手机的界面
    public void selectGoodsByExampleMenu(){
        System.out.println("******************请选择操作******************\n");
        System.out.println("1__按照价格从高到低");
        System.out.println("2__按照价格从低到高");
        System.out.println("3__自定义价格区间");
        System.out.println("4__按照品牌查找手机");
        System.out.println("5__查看商品详细信息");
        System.out.println("0__返回上一级");
        System.out.println("-1__退出");
        System.out.println("\n*************************************************");
    }

    //按照条件查找手机的实际操作
    public void selectGoodsByExample(){
        selectGoodsByExampleMenu();
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                selectGoodsASC();
                selectGoodsByExample();
                break;
            case 2:
                selectGoodsDESC();
                selectGoodsByExample();
                break;
            case 3:
                selectGoodsByPriceRange();
                selectGoodsByExample();
                break;
            case 4:
                selectGoodsByBrand();
                selectGoodsByExample();
                break;
            case 5:
                selectGoodsDetails();
                selectGoodsByExample();
                break;
            case 0:
                clientsMenus();
                break;
            case -1:
                System.exit(0);
                break;
            default:
                System.out.println("输入有误！重新输入！");
                selectGoodsByExample();
        }
    }

    //执行操作1 价格升序
    public void selectGoodsASC(){
        List<Goods> goods = goodsService.selectGoodsASC();
        System.out.println("***************************商品信息如下**************************\n\n");
        for (Goods good : goods) {
            System.out.println("商品编号："+good.getId()+"\t商品名："+good.getName()+"\t价格："+good.getOutputMoney()+"\t品牌："+good.getBrand());
        }
        System.out.println("\n********************************************************************");
    }

    //执行操作2 价格降序
    public void selectGoodsDESC(){
        List<Goods> goods = goodsService.selectGoodsDESC();
        System.out.println("***************************商品信息如下**************************\n\n");
        for (Goods good : goods) {
            System.out.println("商品编号："+good.getId()+"\t商品名："+good.getName()+"\t价格："+good.getOutputMoney()+"\t品牌："+good.getBrand());
        }
        System.out.println("\n********************************************************************");
    }

    //执行操作3 自定义价格区间
    public void selectGoodsByPriceRange(){

        System.out.println("价格最大值为："+goodsService.selectGoodsMaxPrice());
        System.out.println("价格最小值为："+goodsService.selectGoodsMinPrice());

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入起始价格：");
        double beginPrice = scanner.nextDouble();
        System.out.print("请输入最低价格：");
        double endPrice = scanner.nextDouble();

        List<Goods> goods = goodsService.selectGoodsByPrice(beginPrice, endPrice);
        if (goods != null){
            System.out.println("***************************商品信息如下**************************\n\n");
            for (Goods good : goods) {
                System.out.println("商品编号："+good.getId()+"\t商品名："+good.getName()+"\t价格："+good.getOutputMoney()+"\t品牌："+good.getBrand());
            }
            System.out.println("\n********************************************************************");
        }else {
            System.out.println("该价格区间的手机暂时不存在");
        }

    }

    //执行操作4 按品牌查找手机
    public void selectGoodsByBrand(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要查找的手机品牌");
        String brand = scanner.next();

        List<Goods> goods = goodsService.selectGoodsByBrand(brand);
        if (goods != null){
            System.out.println("***************************商品信息如下**************************\n\n");
            for (Goods good : goods) {
                System.out.println("商品编号："+good.getId()+"\t商品名："+good.getName()+"\t价格："+good.getOutputMoney()+"\t品牌："+good.getBrand());
            }
            System.out.println("\n********************************************************************");
        }else {
            System.out.println("暂未找到您想要的品牌！");
        }
    }

    /**
     * 执行操作5 查看商品详细信息
     * 查看商品详细信息，
     * 1.用户可以选择购买，
     * 2.或者加入购物车
     * 3.查看该商品的评论
     */

    public void selectGoodsDetails(){
        System.out.print("输入商品编号，查看商品详细信息:");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        Goods goodsById = goodsService.selectGoodsById(id);
        if (goodsById == null){
            //没有查到相关商品信息
            System.out.println("您输入的商品信息可能不正确，请重新输入！");
            selectGoodsDetails();
        }else {
            System.out.println("*************>>>"+goodsById.getName()+"<<<信息如下*************\n\n");
            System.out.println("编号："+goodsById.getId());
            System.out.println("名称："+goodsById.getName());
            System.out.println("品牌："+goodsById.getBrand());
            System.out.println("售价："+goodsById.getOutputMoney());
            System.out.println("库存："+goodsById.getAmount());
            System.out.println("上架时间："+goodsById.getOnDate());
            System.out.println("商品描述："+goodsById.getDescription());
            handleGoodsMenu(id);
            System.out.println("\n*****************************************************************");
        }
    }

    /**
     * 查看商品详细信息对于商品的处理操作
     * 1.立即购买
     * 2.加入购物车
     * 3.查看该商品的评论
     * 4.发表评论
     */
    public void handleGoodsMenu(String id){
        System.out.println("*******************请选择对应的操作*****************\n");
        System.out.println("1.立即购买");
        System.out.println("2.加入购物车");
        System.out.println("3.查看该商品的评论");
        System.out.println("4.发表评论");
        System.out.println("0.返回上一级");
        System.out.println("-1.退出系统");
        System.out.println("***************************************************");

        Scanner scanner = new Scanner(System.in);

        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                buyOneGood(id);
                break;
            case 2:
                addIntoShopChart(id);
                break;
            case 3:
                lookComments(id);
                break;
            case 4:
                writeComment(id);
                break;
            case 0:
                listGoodsFromClientsViews();
                break;
            case -1:
                System.exit(0);
                break;
            default:
                System.out.println("输入有误,重新输入");
                handleGoodsMenu(id);
        }
    }
    /**
     * 查看商品详细信息对于商品的处理操作    传入参数商品id 用户id
     * 1.立即购买
     *      数据库中商品数量要减 1 将记录写入历史订单表中
     *      检查用户的信息列表中有没有收获地址，如果有
     *      选择收货地址,则完成购买，并显示配送信息
     *      如果没有收货地址，则让用户填写收货地址
     * 2.返回商品列表界面
     */
    public void buyOneGood(String id){
        List<Address> addresses = addressService.selectAddressListWithClient(clientID);
        if (!addresses.isEmpty()){
            //收货地址列表不为空，用户选择收货地址，完成购买,并将购买信息写入历史订单中。

            for (Address address : addresses) {
                System.out.println("编号："+address.getId()+"\t用户名："+address.getClient().getName()+
                        "\t收件人姓名："+address.getAcceptName()+"\t收件人号码:"+address.getTel()+
                        "\t收件地址："+address.getAcceptAddress()
                        );
            }
            Scanner scanner = new Scanner(System.in);
            System.out.print("按33键填写新的地址：按其他键选择现有地址：");

            int choose = scanner.nextInt();

            if (choose == 33){
                System.out.print("请输入收货地址：");
                String acceptAddress = scanner.next();
                System.out.print("请输入收货人姓名：");
                String acceptName = scanner.next();
                System.out.print("请输入收件人号码：");
                String tel = scanner.next();
                addressService.insertAddress(clientID,acceptAddress,tel,acceptName);
                System.out.println("填写成功！");
            }
            List<Address> addresses2 = addressService.selectAddressListWithClient(clientID);
            for (Address address : addresses2) {
                System.out.println("编号："+address.getId()+"\t用户名："+address.getClient().getName()+
                        "\t收件人姓名："+address.getAcceptName()+"\t收件人号码:"+address.getTel()+
                        "\t收件地址："+address.getAcceptAddress()
                );
            }
            System.out.print("输入收货地址编号，选择您的收货地址");
            int addressId = scanner.nextInt();
            System.out.print("请输入商品数量：");
            int goodsCount = scanner.nextInt();
            Goods goods = goodsService.selectGoodsById(id);
            double price = goods.getOutputMoney() * goodsCount;
            double cost = goods.getInputMoney() * goodsCount;

            //将订单信息写入历史订单记录
            goodsService.minusOneGood(id,goodsCount);
            historyOrderService.insertOneData(id,clientID,goodsCount,price,addressId);
            System.out.println("购买成功，支付：："+price+"::元");

            /**
             * 支付成功后将数据写入利润表
             * 先查询以前是否购买过本商品
             * 如果没有购买过则直接插入，
             * 如果购买过则修改相应数据
             */
            boolean exist = profitsService.isExist(id);
            if (exist){
                //存在，需要修改数据
                profitsService.update(id,goodsCount,price,cost,(price - cost));
            }else {
                //不存在，直接插入
                profitsService.insert(id,price, cost, (price - cost),goodsCount);
            }


        }else {
            //用户收货列表为空，则该用户没有填写过收货列表，先让用户填写收货地址，再完成购买
            System.out.println("您还没有收货地址，请先填写收货地址");
            String choose;
            do {
                Scanner scanner = new Scanner(System.in);
                System.out.print("请输入收货地址：");
                String acceptAddress = scanner.next();
                System.out.print("请输入收货人姓名：");
                String acceptName = scanner.next();
                System.out.print("请输入收件人号码：");
                String tel = scanner.next();
                addressService.insertAddress(clientID,acceptAddress,tel,acceptName);
                System.out.println("填写成功！");
                System.out.print("是否继续输入y/n:");
                choose = scanner.next();
            }while ("y".equalsIgnoreCase(choose));
            buyOneGood(id);
        }

    }

    /**
     * 操作2：将商品加入购物车
     * 如果购物车中存在当前浏览的商品，则需要修改商品的数量
     * @param id 传入商品的id
     */
    public void addIntoShopChart(String id){
        //查询购物车中是否有当前用户浏览的当前商品
        Scanner scanner = new Scanner(System.in);
        GoodOrder goodOrder = goodOrderService.isExistCurrentGood(clientID, id);
        if (goodOrder != null){
            //存在
            int goodOrderId = goodOrder.getId();
            System.out.print("请输入需要添加的数量：");
            int newAmount = scanner.nextInt();
            int amount = goodOrder.getAmount() + newAmount;
            goodOrderService.updateGoodAmount(goodOrderId,amount);
            System.out.println("添加成功！");
        }else {
            //不存在
            System.out.print("请输入您要添加的数量：");

            int amount = scanner.nextInt();

            Goods goods = goodsService.selectGoodsById(id);
            double price = goods.getOutputMoney()*amount;

            goodOrderService.insertData(clientID,id,amount,price);
            System.out.println("添加成功！");
            listGoodsFromClientsViews();
        }

    }
    /**
     * 操作3：查看该商品的评论
     * 如果该商品没有评论，则提示用户是否发表评论
     * 用户发表评论：
     *      如果用户曾经购买过该商品，则允许评论，否则不允许
     */
    CommentsService commentsService = new CommentsServiceImpl();
    public void lookComments(String id){
        List<Comments> comments = commentsService.selectByGoodsId(id);
        Goods good = goodsService.selectGoodsById(id);
        if (comments.isEmpty()){
            System.out.println("该商品>>"+good.getName()+"<<暂时没有评论!");
        }else {
            System.out.println("***************************\n");
            System.out.println("共"+commentsService.findAllCommentsByGoodId(id)+"条评论：\t好评："+commentsService.findAllCommentsByGoodId(id)+
                    "\t中评："+commentsService.findAllPlainCommentsById(id)+"\t差评："+commentsService.findAllBadCommentsById(id));
            for (Comments comment : comments) {
                System.out.println(comment.getUsername()+">" +comment.getStars()+"颗星>"
                +comment.getcDate()+">"+comment.getContents());
            }
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("按任意键继续：");
        scanner.nextInt();
        listGoodsFromClientsViews();
    }
    /**
     * 操作4：发表评论
     * 先检查用户是否买过该商品，只有买过才能发表评论
     */
    public void writeComment(String id){
        Boolean flag = historyOrderService.findOrderByClient(id, clientID);
        if (flag){
            //如果当前用户买过该商品，则可以评论
            Clients client = clientsService.findClientById(clientID);
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入分数（1~3）：");
            int stars = scanner.nextInt();
            System.out.print("请输入评论内容：");
            String comments = scanner.next();
            commentsService.insertCommentsOnTable(client.getName(),stars,comments,id);
            System.out.println("评论完成！");
            listGoodsFromClientsViews();
        }
    }

    /**
     * 管理员登录方法
     */
    public void adminLogin(){
        Admin admin = new Admin();
        System.out.print("请输入用户名：");
        Scanner scanner = new Scanner(System.in);
        admin.setName(scanner.next());
        System.out.print("请输入密码：");
        admin.setPassword(scanner.next());
        Admin login = adminService.adminLogin(admin.getName(), admin.getPassword());


        if (login != null){
            //此时登录成功
            System.out.println("登录成功！！！！");

            //登录成功，显示管理员可用菜单
            adminView();

            count = 3;
        }else {
            //此时登录失败
            while (true){
                --count;
                if (count > 0){
                    System.out.println("账号或密码错误！请重新输入！您还有::"+count+"::次机会");
                    adminLogin();
                }else {
                    System.out.println("次数耗尽，已退出！");
                    System.exit(0);
                }
            }

        }
    }
    /**
     * 管理员可用菜单
     */
    public void adminView(){
        System.out.println("输入指令选择操作：");
        System.out.println("1.查看门店销售利润");
        System.out.println("2.查看注册用户");
        System.out.println("3.修改商品");
        System.out.println("4.添加商品");
        System.out.println("0.返回上一级");
        System.out.println("-1.退出系统");
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                saleInfo();
                break;
            case 2:
                lookClients();
                break;
            case 3:
                updateGoods();
                break;
            case 4:
                addGood();
                break;
            case 0:
                mainView();
                break;
            case -1:
                System.exit(0);
                break;
            default:
                System.out.println("输入有误，重新输入");
                adminView();
        }
    }
    /**
     * 操作1：查看门店销售数据
     */
    public void saleInfo(){
        System.out.println("****************************************************************************");
        List<Profits> profits = profitsService.saleInfo();
        for (Profits profit : profits) {
            System.out.println("编号："+profit.getId()+"\t商品名："+profit.getName()+"\t销售量："+profit.getAmount()+"\t总销售额："+
                    profit.getSaleMoney()+"\t总成本："+profit.getCost()+"\t总利润："+profit.getProfit());
        }
        System.out.println("****************************************************************************");

        adminView();
    }

    /**
     * 操作2：查看注册用户
     */
    public void lookClients(){
        System.out.println("***********************************************************");
        List<Clients> clients = clientsService.findAllClients();
        for (Clients client : clients) {
            int id = client.getId();
            String name = client.getName();
            int consumeAmount = historyOrderService.findConsumeAmount(id);
            double consumeMoney = historyOrderService.findConsumeMoney(id);
            System.out.println("姓名："+name+"\t消费次数："+consumeAmount+"\t消费金额："+consumeMoney);
        }
        System.out.println("***********************************************************");
        adminView();

    }
    /**
     * 修改商品信息
     */
    public void updateGoods(){
        System.out.println("**************************************************************************************************************");
        List<Goods> goods = goodsService.selectAllGoods();
        for (Goods good : goods) {
            System.out.println(good);
        }
        System.out.println("**************************************************************************************************************");

        //输入编号查看商品详细信息
        System.out.print("输入编号：");
        Scanner scanner = new Scanner(System.in);
        String goodsId = scanner.next();
        Goods byId = goodsService.selectGoodsById(goodsId);
        String name = byId.getName();
        System.out.println("************************>>"+name+"<<************************");

        updateData(goodsId);

    }
    /**
     * 修改指定属性
     */
    public void updateData(String goodsId){
        System.out.println("1.进价");
        System.out.println("2.售价");
        System.out.println("3.库存");
        System.out.println("4.描述");
        System.out.print("输入您需要修改的属性：");
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
            case 2:
                System.out.print("输入新的属性：");
                double property = scanner.nextDouble();
                goodsService.updateInfo(choose,goodsId,property);
                break;
            case 3:
                System.out.print("输入新的属性：");
                int property2 = scanner.nextInt();
                goodsService.updateInfo(choose,goodsId,property2);
                break;
            case 4:
                System.out.print("输入新的属性：");
                String  property3 = scanner.next();
                goodsService.updateInfo(choose,goodsId,property3);
                break;
            default:
                System.out.println("输入有误，重新输入");
                updateData(goodsId);
        }
        adminView();
    }
    /**
     * 操作4：添加商品
     */
    public void addGood(){
        Goods goods = new Goods();
        Scanner scanner = new Scanner(System.in);
        String choose;
        do {
            System.out.print("输入商品名：");
            goods.setName(scanner.next());
            System.out.print("输入商品进价：");
            goods.setInputMoney(scanner.nextDouble());
            System.out.print("输入商品售价：");
            double price = scanner.nextDouble();
            goods.setOutputMoney(price);
            System.out.print("输入商品品牌：");
            String brand = scanner.next();
            goods.setBrand(brand);

            scanner.nextLine();
            System.out.print("输入商品描述：");
            goods.setDescription(scanner.nextLine());
            System.out.print("输入库存量：");
            goods.setAmount(scanner.nextInt());

            goodsService.insert(goods);

            System.out.println("添加完成，继续添加？y/n");
            choose = scanner.next();
        }while ("y".equalsIgnoreCase(choose));


    }
}
